package com.yxw.blog.controller;

import com.yxw.blog.bean.entity.User;
import com.yxw.blog.bean.response.ResponseCode;
import com.yxw.blog.bean.response.StandardResponse;
import com.yxw.blog.dao.UserDao;
import com.yxw.blog.service.InformationService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.text.ParseException;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

/**
 * @author yexinwei
 * 个人信息控制层
 */
@Api(value = "个人信息控制层")
@Controller()
@RequestMapping("/information")
public class InformationController {

    @Autowired
    UserDao userDao;
    @Autowired
    InformationService informationService;

    /**
     * 头像异步上传
     */
    @ResponseBody
    @RequestMapping("/upload")
    public Map upload(MultipartFile file , HttpSession session){
        //通过session获取userId
        Integer userId = (Integer)session.getAttribute("userId");
        //通过userId获取到user对象
        User user = userDao.getUserById(userId);
        String prefix="";
        //保存上传
        OutputStream out = null;
        InputStream fileInput=null;
        try{
            if(file!=null){
                //获取图片原本的名称
                String originalName = file.getOriginalFilename();
                //获取图片的后缀
                prefix=originalName.substring(originalName.lastIndexOf(".")+1);
                //为图片生成一个名称
                String uuid = UUID.randomUUID()+"";
                //图片存储的路径
                String filepath = System.getProperty("user.dir")+"\\User\\"+user.getUserName()+"\\Head\\"+uuid+"." + prefix;
                //创建文件
                File files=new File(filepath);
                //打印查看上传路径
                //System.out.println(filepath);
                if(!files.getParentFile().exists()){
                    files.getParentFile().mkdirs();
                }
                file.transferTo(files);
                Map<String,Object> map2=new HashMap<>();
                Map<String,Object> map=new HashMap<>();
                map.put("code",0);
                map.put("msg","");
                map.put("data",map2);
                map2.put("src",filepath);
                return map;
            }
        }catch (Exception e){
        }finally{
            try {
                if(out!=null){
                    out.close();
                }
                if(fileInput!=null){
                    fileInput.close();
                }
            } catch (IOException e) {
            }
        }
        Map<String,Object> map=new HashMap<>();
        map.put("code",1);
        map.put("msg","");
        return map;

    }

    /**
     * 显示个人信息
     */
    @ApiOperation(value = "显示个人信息接口")
    @ResponseBody
    @PostMapping("/showInfo")
    public StandardResponse showInfo(HttpSession session) throws IOException {
        Map<String,Object> map = informationService.showInfo(session);
        return new StandardResponse(ResponseCode.SUCCESS,map);
    }

    /**
     * 更新个人信息
     */
    @ApiOperation(value = "更新信息接口")
    @ResponseBody
    @PostMapping("/updateInfo")
    public StandardResponse updateInfo(@RequestParam @ApiParam(value = "昵称") String display_name , @RequestParam @ApiParam(value = "生日") String birthday, @RequestParam @ApiParam(value = "邮箱")  String email, @RequestParam @ApiParam("所在地") String location , @RequestParam @ApiParam(value = "性别") String sex , @RequestParam @ApiParam("头像") String imageUrl , HttpSession session  ) throws ParseException {
        informationService.updateInfo( display_name, birthday , email, location, sex, imageUrl, session);
        return new StandardResponse(ResponseCode.SUCCESS,null);
    }
}
